package com.cuiyun97.result;

import com.cuiyun97.constant.AdminConstant;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回前端请求 (封装类)
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = 0;
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.data = data;
        result.code = 0;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<>();
        result.msg = msg;
        result.code = -1;
        return result;
    }

    public static <T> Result<T> tokenError() {
        Result<T> result = new Result<>();
        result.msg = AdminConstant.NO_LOGIN;
        result.code = AdminConstant.NO_LOGIN_CODE;
        return result;
    }
}
